use std::path::PathBuf;

use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug)]
pub struct RConfig {
    mingw_path: Option<String>,
}

impl RConfig {
    /// 从config.json中读取配置文件
    pub fn read() -> Self {
        let path = super::get_home_path().join("config.json");
        let json = std::fs::read(path).unwrap();
        let json = String::from_utf8(json).unwrap();
        let ans = serde_json::from_str(&json).unwrap();
        return ans;
    }

    /// 从config.json中获取mingw文件夹名称并返回其bin目录
    pub fn get_bin_path(&self) -> Option<PathBuf> {
        if let Some(dir_name) = &self.mingw_path {
            // 路径为: rust-cmaker/mingw_path/bin
            let path: PathBuf = super::get_home_path().join(dir_name).join("bin");
            return Some(path);
        }
        return None;
    }
}
